package com.etc.traval.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.etc.traval.dao.UserDao;
import com.etc.traval.util.JDBCUtil;
import com.etc.travel.entity.User;

import oracle.jdbc.internal.OracleTypes;

public class UserDaoImpl implements UserDao{

	JDBCUtil util = new JDBCUtil();
	
	/**用户登录的方法
	 * @author 刘一清
	 * @param id  用户账号  pwd  用户密码
	 * @return user 该用户信息
	 */
	@Override
	public User UserLogin(int id, String pwd) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		Object[] obj = util.callHasReturn("{call proc_userlogin(?,?,?)}", list,id,pwd);
		ResultSet rs = (ResultSet) obj[0];
		User user= null;
		try {
			if(rs.next()){
				user = new User();
				user.setUser_id(rs.getInt("user_id"));
				user.setUser_name(rs.getString("user_name"));
				user.setUser_pwd(rs.getString("user_pwd"));
				user.setUser_sex(rs.getString("user_sex"));
				user.setUser_tel(rs.getString("user_tel"));
				user.setUser_logo(rs.getString("user_logo"));
				user.setUser_money(rs.getDouble("user_money"));
				user.setUser_status(rs.getString("user_status"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				util.closeAll();
			} catch (Exception e2) {
			}
		}
		return user;
	}

	/**
	 * 注册用户的方法
	 * @author cjw
	 * @param user  注册用户的信息
	 * @return user2 返回该注册用户的信息
	 */
	@Override
	public boolean UserRegister(User user) {
		int i=util.update("insert into t_user(user_name,user_pwd,user_tel,user_sex,user_money,user_logo,user_status) values (?,?,?,?,?,?,?)", user.getUser_name(),user.getUser_pwd(),user.getUser_tel(),user.getUser_sex(),user.getUser_money(),user.getUser_logo(),user.getUser_status());
		return i>0;
	}

	/**
	 * 根据用户id查用户信息
	 * @author cjw
	 * @param id 要查找的用户id
	 * @return user 要查找的用户信息
	 */
	@Override
	public User queryUserById(int id) {
		List<Integer> list=new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		Object[] obj  =  util.callHasReturn("{call  proc_queryuserbyid(?,?)}", list, id);
		ResultSet rs = (ResultSet) obj[0];
		User user= null;
		try {
			if(rs.next()){
				user = new User();
				user.setUser_id(rs.getInt("user_id"));
				user.setUser_name(rs.getString("user_name"));
				user.setUser_pwd(rs.getString("user_pwd"));
				user.setUser_sex(rs.getString("user_sex"));
				user.setUser_tel(rs.getString("user_tel"));
				user.setUser_logo(rs.getString("user_logo"));
				user.setUser_money(rs.getDouble("user_money"));
				user.setUser_status(rs.getString("user_status"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				util.closeAll();
			} catch (Exception e2) {
			}
		}
		return user;
	}

	/**
	 * 根据用户ID修改密码
	 * @author cjw
	 * @param id 用户ID
	 * @param oldpwd 用户的旧密码
	 * @param newpwd 用户的新密码
	 * @return i>0 数据库执行成功行数是否大于0
	 */
	@Override
	public boolean updatePwdById(int id, String oldpwd,String newpwd) {
		List<Integer> list=new ArrayList<Integer>();
		list.add(Types.INTEGER);
		Object[] obj = util.callHasReturn("{call proc_updatepwdbyid(?,?,?,?)}", list,newpwd,id,oldpwd);
		int i = (Integer) obj[0];	
		util.closeAll();
		return i>0;
	}

	/**
	 * 用户充值的方法
	 * @author cjw
	 * @param id 用户ID
	 * @param money 要充值的金额
	 * @return i>0 数据库执行成功行数是否大于0
	 */
	@Override
	public boolean addUserMoneyById(int id, double money) {
		List<Integer> list=new ArrayList<Integer>();
		list.add(Types.INTEGER);
		Object[] obj = util.callHasReturn("{call proc_addUserMoneyById(?,?,?)}",list,money,id);
		int i = (Integer) obj[0];	
		util.closeAll();
		return i>0;
	}

	/**
	 * 用户提现的方法
	 * @author cjw
	 * @param id 用户ID
	 * @param money 要提现的金额
	 * @return i>0 数据库执行成功行数是否大于0
	 */
	@Override
	public boolean reduceUserMoneyById(int id, double money) {
		List<Integer> list=new ArrayList<Integer>();
		list.add(Types.INTEGER);
		Object[] obj = util.callHasReturn("{call proc_reduceUserMoneyById(?,?,?)}",list,money,id);
		int i = (Integer) obj[0];	
		util.closeAll();
		return i>0;
	}

	@Override
	public User queryUserByName(String name) {
			ResultSet rs = util.query("select * from t_user where user_name=?",name);
			User user = null;
			try {
				if (rs.next()) {
					user = new User();
					user.setUser_id(rs.getInt("user_id"));
					user.setUser_name(rs.getString("user_name"));
					user.setUser_sex(rs.getString("user_sex"));
					user.setUser_tel(rs.getString("user_tel"));
					user.setUser_money(rs.getDouble("user_money"));
					user.setUser_pwd(rs.getString("user_pwd"));
					user.setUser_logo(rs.getString("user_logo"));
					user.setUser_status(rs.getString("user_status"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				util.closeAll();
			}
			return user;
	}

	@Override
	public User queryUserByTel(String tel) {
	ResultSet rs=util.query("select * from t_user where user_tel=?", tel);
	User user = null;
	try {
		if (rs.next()) {
			user = new User();
			user.setUser_id(rs.getInt("user_id"));
			user.setUser_name(rs.getString("user_name"));
			user.setUser_sex(rs.getString("user_sex"));
			user.setUser_tel(rs.getString("user_tel"));
			user.setUser_money(rs.getDouble("user_money"));
			user.setUser_pwd(rs.getString("user_pwd"));
			user.setUser_logo(rs.getString("user_logo"));
			user.setUser_status(rs.getString("user_status"));
		}
	} catch (SQLException e) {
		e.printStackTrace();
	} finally {
		util.closeAll();
	}
	return user;
	}

	@Override
	public int checktel(String tel) {
		ResultSet rs=util.query("select user_name  from t_user where user_tel = ?", tel);
		String t=null;
		try {
			if(rs.next()){
				t=rs.getString("user_name");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			util.closeAll();
		}
		if(t==null){
			return 0;
		}else{
		return 1;
	}
	}

	/**
	 * 查找所有用户
	 * @author cjw
	 */
	@Override
	public List<User> queryalluser() {
		List<User> list = new ArrayList<User>();
		ResultSet rs = util.query("select *from t_user");
		try {
			while(rs.next()) {
				User user = new User();
				user = new User();
				user.setUser_id(rs.getInt("user_id"));
				user.setUser_name(rs.getString("user_name"));
				user.setUser_sex(rs.getString("user_sex"));
				user.setUser_tel(rs.getString("user_tel"));
				user.setUser_money(rs.getDouble("user_money"));
				user.setUser_pwd(rs.getString("user_pwd"));
				user.setUser_logo(rs.getString("user_logo"));
				user.setUser_status(rs.getString("user_status"));
				user.setUser_register(rs.getTimestamp("user_register"));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			util.closeAll();
		}
		return list;
	}

	@Override
	public boolean updatePwdById(int id, String newpwd) {
	int i=util.update("update t_user set user_pwd=?  where user_id=?",newpwd,id);
		return i>0;
	}

}
